package de.hijacksoft.isle.classes;

import com.sun.jndi.toolkit.url.Uri;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * This class represents any item in the application
 * @author HiJack Soft
 */
public class Item implements Comparable<Item> {

    public String name = null;
    public Category category = new Category(Category.UNCATEGORIZED, "", 0xaaaaaa);
    public double amount, price = 0;
    public char unit = 'p';
    public Uri image = null;
    public long lastbought = 0;
    public float avgperiod = 2;
    public boolean bought = false;
    public int id = -1;
    public int boughtcount = 0;
    public int historyid = -1337;
    public long barcode = 0;
    // Sort Mode
    public static final int SORT_ALPHA = 0;
    public static final int SORT_LASTBOUGHT = 1;
    public static final int SORT_PERIOD = 2;
    public static final int SORT_HISTORY = 3;
    private static int sortmode = SORT_ALPHA;

    /**
     * Sets the way items are being sorted
     * @param mode possible modes are SORT_ALPHA, SORT_LASTBOUGHT, SORT_PERIOD, SORT_HISTORY
     */
    public static void setSortMode(int mode) {
        sortmode = mode;
    }

    /**
     * Gets the way items are being sorted
     * @return returns the mode
     */
    public static int getSortMode() {
        return sortmode;
    }

    public Item() {
    }

    public Item(String name, double amount, double price, char unit, Uri image, long lastbought, float avgperiod, int recipeid) {
        this.name = name;
        this.amount = amount;
        this.price = price;
        this.unit = unit;
        this.image = image;
        this.lastbought = lastbought;
        this.avgperiod = avgperiod;
    }

    public Item(String name, double amount, double price, char unit, Uri image, long lastbought, float avgperiod) {
        this.name = name;
        this.amount = amount;
        this.price = price;
        this.unit = unit;
        this.image = image;
        this.lastbought = lastbought;
        this.avgperiod = avgperiod;
    }

    public Item(String name, double amount, double price, char unit, Uri image) {
        this.name = name;
        this.amount = amount;
        this.price = price;
        this.unit = unit;
        this.image = image;
        this.avgperiod = 2;
    }
    
    public static Item getItemFromString(String str){
        
        Pattern p = Pattern.compile("[äöüÄÖÜß'\\.\\(\\)\\-\\d\\n\\w ]*, ");
        Matcher m = p.matcher(str);
        
        ArrayList<String> groups = new ArrayList<String>();
        
        while(m.find()){
            groups.add(m.group().replaceAll(", ", ""));
        }
        
        if(groups.size() > 0){
        
            Item item = new Item();
            item.id = Integer.parseInt(groups.get(0));
            item.name = groups.get(1);
            item.amount = Double.parseDouble(groups.get(2).substring(0, groups.get(2).length() - 2));
            item.unit = groups.get(2).charAt(groups.get(2).length() - 1);
            item.price = Double.parseDouble(groups.get(3));
            item.lastbought = Long.parseLong(groups.get(4));
            item.avgperiod = (int)Double.parseDouble(groups.get(5));
            item.barcode = Long.parseLong(groups.get(6));
            item.category = new Category(Integer.parseInt(groups.get(7)), "", 0);        
            item.boughtcount = Integer.parseInt(groups.get(8));        
        
            return item;

        }
        else{
            return null;
        }
        
    }

    @Override
    public String toString() {
        return id + ", " + name + ", " + amount + unit + ", " + price + ", " + lastbought + ", " + avgperiod + ", " + barcode + ", " + category.id + ", "  + boughtcount + ", ";
    }

    @Override
    // This method controls the way items are being sorted
    public int compareTo(Item another) {
        switch (sortmode) {
            case SORT_ALPHA:
                return name.compareTo(another.name);
            case SORT_HISTORY:
                return another.historyid - historyid;
            case SORT_LASTBOUGHT:
                return (int) (another.lastbought - lastbought);
            case SORT_PERIOD:
                return (int) (avgperiod * 10 - another.avgperiod * 10);
            default:
                return name.compareTo(another.name);
        }
    }
}
